package com.sky.controller.admin;

import com.sky.Result.Result;
import com.sky.constant.JwtClaimsConstant;
import com.sky.model.dto.AdminDTO;
import com.sky.model.dto.AdminLoginDTO;
import com.sky.model.entity.Admin;
import com.sky.model.entity.User;
import com.sky.model.vo.AdminLoginVO;
import com.sky.properties.JwtProperties;
import com.sky.service.AdminService;
import com.sky.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/admin")
@Slf4j
@CrossOrigin
public class AdminController {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private AdminService adminService;
    /**
     * 登录
     * @param adminLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value ="管理员登录")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
        log.info("管理员登录：{}", adminLoginDTO);
        Admin admin = adminService.login(adminLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ADMIN_ID, admin.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .id(admin.getId())
                .userName(admin.getUsername())
                .name(admin.getName())
                .token(token)
                .build();
        return Result.success(adminLoginVO);
    }
    /**
     * 修改个人信息
     * @param adminDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody AdminDTO adminDTO) {
            log.info("修改用户信息：userDTO{}", adminDTO);
        adminService.update(adminDTO);
        return Result.success();
    }
}
